// miniprogram/pages/home/message/message/contact.js
const app = getApp();
var inputVal = '';
var msgList = [];
var windowWidth = wx.getSystemInfoSync().windowWidth;
var windowHeight = wx.getSystemInfoSync().windowHeight;
var keyHeight = 0;

/**
 * 初始化数据
 */
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollHeight: '100vh',
    inputBottom: 0,
    inputVal : '',
    leftAvatar:'',
    rightAvatar: '',
    contactUniqueId:null,
    toView: 'msg-' + (msgList.length - 1),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //消除未读消息小红点
    wx.request({
      url: app.globalData.url + '/msg/cleanRead',
      data: {
        uniqueId: app.globalData.uniqueId,
        contactUniqueId: options.contactUniqueId
      },
      success: res => {
        if (res.code == 0) {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
          })
        }
      }
    })
    //获取消息
    wx.request({
      url: app.globalData.url + '/msg/getMessage',
      data: {
        uniqueId: options.uniqueId,
        contactUniqueId: options.contactUniqueId
      },
      success: res => {
        that.setData({
          contactUniqueId:options.contactUniqueId,
          msgList: res.data,
          inputVal: '',
          leftAvatar: options.avatarUrl,
          rightAvatar: app.globalData.userInfo.avatarUrl,
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  onReady:function(){
    var that = this
    that.setData({
      toView: 'msg-' + (this.data.msgList.length - 1),      
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 获取聚焦
   */
  focus: function (e) {
    keyHeight = e.detail.height;
    this.setData({
      scrollHeight: (windowHeight - keyHeight) + 'px'
    });
    this.setData({
      toView: 'msg-' + (msgList.length - 1),
      inputBottom: keyHeight + 'px'
    })
    //计算msg高度
    // calScrollHeight(this, keyHeight);

  },

  //失去聚焦(软键盘消失)
  blur: function (e) {
    this.setData({
      scrollHeight: '100vh',
      inputBottom: 0
    })
    this.setData({
      toView: 'msg-' + (msgList.length - 1)
    })

  },

  /**
   * 发送点击监听
   */
  sendClick: function (e) {
    var that = this
    if (e.detail.value==''){

    }else{
      wx.request({
        url: app.globalData.url + '/msg/sendMsg',
        data: {
          uniqueId: app.globalData.uniqueId,
          contactUniqueId: this.data.contactUniqueId,
          text: e.detail.value
        }
        , success: res=>{
          if (res.data.code == 0) {
            wx.showModal({
              title: '提示',
              content: res.data.msg,
            })
          } else {
            wx.request({
              url: app.globalData.url + '/msg/getMessage',
              data: {
                uniqueId: app.globalData.uniqueId,
                contactUniqueId: that.data.contactUniqueId
              },
              success: res => {
                that.setData({
                  toView: 'msg-' + (res.data.length - 1),
                  msgList: res.data,
                  inputVal: '',
                })
              }
            })
          }
        }
      })
    }
  },

  /**
   * 退回上一页
   */
  toBackClick: function () {
    wx.navigateBack({})
  }

})